/*  
 * Copyright 2007 MBARI 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */

package org.mbari.timeline.piccolo;

import java.io.BufferedReader;
import java.io.StringReader;

import org.mbari.timeline.DateUtil;
import org.mbari.timeline.piccolo.TimeSeries;

import junit.framework.TestCase;

import com.domainlanguage.time.Duration;
import com.domainlanguage.time.TimePoint;

/** 
 * @author Andrew Chase
 */
public class TestTideNode extends TestCase{
    private TimeSeries tideNode;
    TimePoint fifthTimepoint;

    public void setUp(){
        String tideData = "UTCDate UTCTime Date    Time    Tide    Units\n" + 
            "2007-1-1    08:00:00    2007-1-1    00:00:00    +0.82   Meters\n" +
            "2007-1-1    08:15:00    2007-1-1    00:15:00    +0.80   Meters\n" +
            "2007-1-1    08:30:00    2007-1-1    00:30:00    +0.78   Meters\n" +
            "2007-1-1    08:45:00    2007-1-1    00:45:00    +0.76   Meters\n" +
            "2007-1-1    09:00:00    2007-1-1    01:00:00    +0.75   Meters\n" +
            "2007-1-1    09:15:00    2007-1-1    01:15:00    +0.75   Meters\n" +
            "2007-1-1    09:30:00    2007-1-1    01:30:00    +0.75   Meters\n" +
            "2007-1-1    09:45:00    2007-1-1    01:45:00    +0.76   Meters\n" +
            "2007-1-1    10:00:00    2007-1-1    02:00:00    +0.78   Meters\n" +
            "2007-1-1    10:15:00    2007-1-1    02:15:00    +0.81   Meters\n" +
            "2007-1-1    10:30:00    2007-1-1    02:30:00    +0.84   Meters\n" +
            "2007-1-1    10:45:00    2007-1-1    02:45:00    +0.88   Meters\n" +
            "2007-1-1    11:00:00    2007-1-1    03:00:00    +0.92   Meters\n" +
            "2007-1-1    11:15:00    2007-1-1    03:15:00    +0.97   Meters\n" +
            "2007-1-1    11:30:00    2007-1-1    03:30:00    +1.03   Meters\n" +
            "2007-1-1    11:45:00    2007-1-1    03:45:00    +1.10   Meters\n" +
            "2007-1-1    12:00:00    2007-1-1    04:00:00    +1.16   Meters\n" +
            "2007-1-1    12:15:00    2007-1-1    04:15:00    +1.23   Meters\n" +
            "2007-1-1    12:30:00    2007-1-1    04:30:00    +1.31   Meters\n" +
            "2007-1-1    12:45:00    2007-1-1    04:45:00    +1.38   Meters\n" +
            "2007-1-1    13:00:00    2007-1-1    05:00:00    +1.46   Meters\n" +
            "2007-1-1    13:15:00    2007-1-1    05:15:00    +1.53   Meters\n";
        BufferedReader reader = new BufferedReader(new StringReader(tideData));
        tideNode = TimeSeries.makeTideNode(reader, "\\s+");
        fifthTimepoint = DateUtil.ansiStandardDateStringToDate("20070101-09:15:00"); 
    }
    
    public void testGetOffsetIntoArrayBadParam() throws Exception{
        TimePoint tooEarly = DateUtil.ansiStandardDateStringToDate("20070101-01:01:01");
        try{
            tideNode.getOffsetIntoArray(tooEarly);
            fail("expected exception was never thrown");
        }catch(Exception e){
            //expected
        }
    }
    
    public void testGetClosestTideToTime() throws Exception{
        assertTrue(.75f == tideNode.getClosestValueToTime(fifthTimepoint));
        TimePoint lastTime = DateUtil.ansiStandardDateStringToDate("20070101-13:15:00");
        float tide = tideNode.getClosestValueToTime(lastTime);
        assertTrue("Tide value is " + tide, 1.53f == tide);
    }
    
    public void testGetOffsetIntoArray() throws Exception{
     
        int offset = tideNode.getOffsetIntoArray(fifthTimepoint);
        assertEquals(5, offset);
        TimePoint closeToFifth = fifthTimepoint.plus(Duration.minutes(7));
        assertEquals(5, tideNode.getOffsetIntoArray(closeToFifth));
        TimePoint closeToSix = fifthTimepoint.plus(Duration.minutes(13));
        assertEquals(6, tideNode.getOffsetIntoArray(closeToSix));
    }
}
